package com.example.administrator.comdlfmytest;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private TextView title;
    private TextView back_btn, register_btn, zhpsw;
    private Button login_btn;
    private EditText user_name_input, passwd_input;
    private String userName, psw, passwd_again_input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

    private void init() {
        title = (TextView) findViewById(R.id.title);
        title.setText("登录");
        back_btn = (TextView) findViewById(R.id.back_btn);
        register_btn = (TextView) findViewById(R.id.register_btn);
        zhpsw = (TextView) findViewById(R.id.zhpsw);
        login_btn = (Button) findViewById(R.id.login_btn);
        user_name_input = (EditText) findViewById(R.id.user_name_input);
        passwd_input = (EditText) findViewById(R.id.passwd_input);

        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginActivity.this.finish();
            }
        });

        //立即注册控件的点击事件
        register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1); //启动某个页面，并且要求他给我返回结果
            }
        });


        //登录按钮的点击事件
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userName = user_name_input.getText().toString().trim();
                psw = passwd_input.getText().toString().trim();  //当前用户输入的密码

                passwd_again_input = readPsw(userName); //把已经存起来密码拿出来，进行比较
                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(psw)) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (passwd_input.equals(passwd_again_input)) {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    saveLoginStatus(true, userName);
                    Intent data = new Intent();


                    LoginActivity.this.finish();

                    return;
                } else if ((passwd_again_input != null && !TextUtils.isEmpty(passwd_again_input) && !passwd_input.equals(passwd_again_input))) {
                    Toast.makeText(LoginActivity.this, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    Toast.makeText(LoginActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void saveLoginStatus(boolean status,String userName){
        //loginInfo表示文件名
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean("isLogin", status);
        editor.putString("loginUserName", userName);
        editor.commit();
    }

    private String readPsw(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.getString(userName, "");
    }

    //如果不采用startActivityForResult 的方式， 是否有办法在两个activity之间进行数据交互
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            //从注册界面传递过来的用户名
            String userName = data.getStringExtra("userName");
            if (!TextUtils.isEmpty(userName)) {
                user_name_input.setText(userName);
                //设置光标的位置
                user_name_input.setSelection(userName.length());
            }
        }
    }
}
